package mq.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * @author shanzhenbo
 */
public class MyDefaultConsumer extends DefaultConsumer {

	private String consumerName;

	public MyDefaultConsumer(Channel channel, String name) {
		super(channel);
		consumerName = name;
	}

	/**
	 * @param consumerTag 消息者标签，在channel.basicConsumer时候可以指定
	 * @param envelope    消息包的内容，可从中获取消息id，消息routingkey，交换机，消息和重传标志
	 * @param properties  属性消息
	 * @param body        消息
	 * @throws IOException
	 */
	@Override
	public void handleDelivery(String consumerTag, Envelope envelope,
	                           AMQP.BasicProperties properties, byte[] body) throws IOException {
		System.out.println("路由key为：" + envelope.getRoutingKey());
		System.out.println("交换机为：" + envelope.getExchange());
		System.out.println("消息ID为：" + envelope.getDeliveryTag());
		System.out.println(consumerName + "-接收到的消息为：" + new String(body, "utf-8"));
	}
}
